package com.abbas.CayTcore.entity

import androidx.room.ColumnInfo
import androidx.room.Embedded
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.abbas.CayTcore.converter.GZConverter

@Entity(tableName = "tCore")
data class TCoreBase(
    @ColumnInfo
    var length: Int,
    @ColumnInfo
    var width: Int,
    @ColumnInfo
    var height: Int
) {

    @PrimaryKey(autoGenerate = true)
    var _id: Int? = null

    @ColumnInfo
    var xh: String? = null

    @ColumnInfo
    var gz: Float? = null

    @ColumnInfo
    var qs: Float? = null

    @ColumnInfo
    var dz: Float = 0.0f

    @Embedded
    var wireRod: WireRod = WireRod(0f,0f)

    constructor(
        length: Int,
        width: Int,
        height: Int,
        gz: Float
    ) : this(length, width, height) {
        this.gz = gz
    }

    constructor(
        length: Int,
        width: Int,
        height: Int,
        gz: Float,
        xh: String,
        wireRod: WireRod,
        qs: Float,
    ): this(length, width, height,gz){
        this.xh = xh
        this.wireRod = wireRod
        this.qs = qs
    }

    fun getHG() = "%02d".format(height) + "-" + GZConverter.convertGz(gz!!)

    fun getLW() = "%02d".format(length) + "%02d".format(width)


    override fun toString(): String {
//        return if (gz == null)
//            "$length$width$height-???"
//        else
        var format = "%02d".format(height)
        return  "$length$width$format" + "-" + GZConverter.convertGz(gz!!)
    }

    fun parse(name: String) {
        length = name.substring(0, 2).toInt()
        width = name.substring(2, 4).toInt()
        height = name.substring(4, 6).toInt()
        gz = name.split("-")[1]
            .replace("R", ".")
            .toFloat()
    }

    companion object{

    }
}